/*
 * TreeTest.fx
 *
 * Created on 2009-7-16, 14:01:04
 */

package com.sjtu.fx.test;

import javafx.scene.CustomNode;

import javafx.scene.Node;

import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.ext.swing.SwingComponent;


import javax.swing.JTree;


import javax.swing.tree.DefaultMutableTreeNode;

import javax.swing.tree.DefaultTreeModel;

import javax.swing.JScrollPane;

import javafx.io.http.HttpRequest;
import java.io.DataInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.System;

import org.json.JSONObject;

import javax.swing.JButton;






/**
 * @author Administrator
 */
public class TreeTest extends CustomNode{
   
   var root:DefaultMutableTreeNode = new DefaultMutableTreeNode("Root");
   var treeModel:DefaultTreeModel = new DefaultTreeModel(root);
    var tree:JTree = new JTree(treeModel);
    var scrollPane:JScrollPane = new JScrollPane(tree);


 function test(){
       System.out.println("test begin");
   //    var root:DefaultMutableTreeNode = new DefaultMutableTreeNode("Root");
       var subroot:DefaultMutableTreeNode  = new DefaultMutableTreeNode("SubRoot");
       var leaf1: DefaultMutableTreeNode  = new DefaultMutableTreeNode("Leaf 1");
       var leaf2: DefaultMutableTreeNode  = new DefaultMutableTreeNode("Leaf 2");
    //   treeModel = new DefaultTreeModel(root);
       subroot.add(leaf1);
       root.add(leaf2);
       treeModel.insertNodeInto(subroot, root, 0);
  //   tree = new JTree(treeModel);
  //   scrollPane = new JScrollPane(tree);
       scrollPane.repaint();
       System.out.println("test end");
    }

  override protected function create() : Node {
      callcategory();
        Group{
            content:[
              //  myTree,
                SwingComponent.wrap(scrollPane)
            ]
        }
     }


 function run() {
    Stage {
       width: 420 height: 520
        scene: Scene {
            content: [
                TreeTest {translateX: 40 translateY: 40}
            ]
        }
    }
}

function callcategory(){
    HttpRequest {
        method:HttpRequest.POST
        location:"http://localhost:8764/JavaFXBack/MainCategory"
       onOutput: function(os:OutputStream) {
             try {
                os.write("1".getBytes());
                os.flush();
            } finally {
                os.close();
            }
        }
        onInput: function(is: InputStream) {
        try {
                def data:DataInputStream = new DataInputStream(is);
                var result:String = data.readLine();
                System.out.println(result);
                var json:JSONObject = new JSONObject(result);
                System.out.println(json.getJSONArray("mainCategory"));
                var btu:JButton = new JButton("aaa");
                btu.setVisible(true);
                System.out.println(btu.getWidth());
                scrollPane.add(btu);
            } finally {
                test();
                is.close();
            }
        }
    }.start();
    
}

}